//
//  CitiesView.m
//  CitySelected
//
//  Created by admin on 16/3/21.
//  Copyright © 2016年 sigxui-001. All rights reserved.
//

#import "CitiesView.h"
#import "YSCAreaView.h"
#import "ZXAreaModel.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface CitiesView ()<UITableViewDataSource,UITableViewDelegate>
{
    //NSArray *arrayList;
}
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <ZXCityModel *>*arrayList;

@property (nonatomic, strong) NSMutableArray <ZXRegionModel *>*RegionList;

@end
@implementation CitiesView


- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH-100, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(100, 0, SCREEN_WIDTH-100, SCREEN_HEIGHT);
    }
    return self;
}




- (void)setProvId:(NSString *)provId{
    WeakSelf(WSelf);
    [[ZXHTTPManager sharedManager]getCityInfoProvId:provId Completed:^(NSDictionary *jsonDict, NSError *error) {
//        NSLog(@"%@",jsonDict);
        WSelf.arrayList = [ZXCityModel mj_objectArrayWithKeyValuesArray:jsonDict[@"result"]];
        [self addSubview:self.tableView];
    }];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayList.count ;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = self.arrayList[indexPath.row].cityName;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WeakSelf(WSelf);
    [[ZXHTTPManager sharedManager]getRegionInfoCityId:self.arrayList[indexPath.row].cityId Completed:^(NSDictionary *jsonDict, NSError *error) {
//        NSLog(@"%@",jsonDict);
        WSelf.RegionList = [ZXRegionModel mj_objectArrayWithKeyValuesArray:jsonDict[@"result"]];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];

        if (WSelf.RegionList.count > 0) {
            YSCAreaView *regionView = [[YSCAreaView alloc]init];
            
            //tianjia table
            [regionView getArea:WSelf.RegionList];
            
            regionView.block = ^(NSMutableDictionary *dict){

                dic[@"regionName"] = dict[@"regionName"];
                dic[@"regionCode"] = dict[@"regionCode"];
                dic[@"cityName"] = self.arrayList[indexPath.row].cityName;
                dic[@"cityCode"] = self.arrayList[indexPath.row].cityCode;
                
                self.blockCity((NSMutableDictionary *)dic);
            };
            dispatch_async(dispatch_get_main_queue(), ^{
                [WSelf addSubview:regionView];
            });
            
        }else{
//            NSMutableDictionary *dic = @{
//                                  @"city":self.arrayList[indexPath.row].cityName,
////                                  @"cityCode":self.arrayList[indexPath.row].cityCode
//                                  };
//            self.blockCity((NSMutableDictionary *)dic);
        }        
    }];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
